package net.opentsdb.tsd;

import com.stumbleupon.async.Deferred;
import net.opentsdb.core.IncomingDataPoint;
import net.opentsdb.core.TSDB;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Hujf
 * @title: StatsRpc
 * @date 2021-09-14 15:16
 * @description: 处理执行请求类
 */
public class StatsRpc implements TelnetRpc,HttpRpc {
    @Override
    public Deferred<Object> execute(TSDB tsdb, Channel chan, String[] command) {
        boolean canonical = tsdb.getConfig().getBoolean("tsd.stats.canonical");
        StringBuilder buf = new StringBuilder(1024);
        //ASCIICollector collector = new ASCIICollector("tsd", buf, null);
        return null;
    }

    @Override
    public void execute(TSDB tsdb, HttpQuery query) {
        // only accept GET/POST
        if (query.method() != HttpMethod.GET && query.method() != HttpMethod.POST) {
            throw new BadRequestException(HttpResponseStatus.METHOD_NOT_ALLOWED,
                    "Method not allowed", "The HTTP method [" + query.method().getName() +
                    "] is not permitted for this endpoint");
        }

        String[] uri = query.explodeAPIPath();

        final List<IncomingDataPoint> dps = new ArrayList<IncomingDataPoint>(64);
        tsdb.collectStats();
       query.sendReply(ChannelBuffers.copiedBuffer("ok".getBytes(StandardCharsets.UTF_8)));
    }
}
